﻿// Decompiled with JetBrains decompiler
// Type: MCM.Abstractions.SettingsPropertyGroupDefinition
// Assembly: MCMv5, Version=5.10.2.0, Culture=neutral, PublicKeyToken=null
// MVID: 7AB326B9-B2B4-41C2-9177-68E944071101
// Assembly location: D:\steam\steamapps\workshop\content\261550\2859238197\bin\Win64_Shipping_Client\MCMv5.dll

using MCM.Common;
using System;
using System.Collections.Generic;
using System.Linq;

#nullable enable
namespace MCM.Abstractions
{
  public class SettingsPropertyGroupDefinition
  {
    public static readonly string DefaultGroupName = "{=SettingsPropertyGroupDefinition_Misc}Misc";
    public static readonly IPropertyGroupDefinition DefaultGroup = (IPropertyGroupDefinition) new SettingsPropertyGroupDefinition.DefaultPropertyGroupDefinition();
    protected readonly string _groupNameRaw;
    protected readonly string _groupNameOverrideRaw = string.Empty;
    protected readonly List<SettingsPropertyGroupDefinition> subGroups = new List<SettingsPropertyGroupDefinition>();
    protected readonly List<ISettingsPropertyDefinition> settingProperties = new List<ISettingsPropertyDefinition>();

    protected char SubGroupDelimiter { get; set; }

    public SettingsPropertyGroupDefinition? Parent { get; set; }

    public string GroupNameRaw => this._groupNameRaw;

    public string GroupName => this.DisplayGroupNameRaw;

    public string DisplayGroupNameRaw
    {
      get
      {
        if (this.Parent == null)
          return LocalizationUtils.Localize(this._groupNameRaw);
        string oldValue = LocalizationUtils.Localize(this.Parent._groupNameRaw);
        return LocalizationUtils.Localize(this._groupNameRaw).Replace(oldValue, string.Empty).TrimStart(this.SubGroupDelimiter);
      }
    }

    public int Order { get; private set; }

    public IEnumerable<SettingsPropertyGroupDefinition> SubGroups
    {
      get => (IEnumerable<SettingsPropertyGroupDefinition>) this.subGroups.SortDefault();
    }

    public IEnumerable<ISettingsPropertyDefinition> SettingProperties
    {
      get => (IEnumerable<ISettingsPropertyDefinition>) this.settingProperties.SortDefault();
    }

    public bool IsEmpty
    {
      get
      {
        return !this.SubGroups.Any<SettingsPropertyGroupDefinition>() && this.SettingProperties.All<ISettingsPropertyDefinition>((Func<ISettingsPropertyDefinition, bool>) (x => x.SettingType == SettingType.NONE));
      }
    }

    public SettingsPropertyGroupDefinition(string groupName, int order = -1)
    {
      this._groupNameRaw = groupName;
      this.Order = order;
    }

    public SettingsPropertyGroupDefinition SetParent(SettingsPropertyGroupDefinition parent)
    {
      this.Parent = parent;
      return this;
    }

    public SettingsPropertyGroupDefinition SetSubGroupDelimiter(char subGroupDelimiter)
    {
      this.SubGroupDelimiter = subGroupDelimiter;
      return this;
    }

    public void Add(ISettingsPropertyDefinition settingProp)
    {
      this.settingProperties.Add(settingProp);
    }

    public void Add(SettingsPropertyGroupDefinition settingProp)
    {
      this.subGroups.Add(settingProp.SetParent(this));
    }

    public SettingsPropertyGroupDefinition? GetGroup(string groupName)
    {
      return this.subGroups.GetGroupFromName(groupName);
    }

    public override string ToString() => this.GroupName;

    public SettingsPropertyGroupDefinition Clone(bool keepRefs = true)
    {
      SettingsPropertyGroupDefinition propertyGroupDefinition = new SettingsPropertyGroupDefinition(this.GroupName, this.Order).SetSubGroupDelimiter(this.SubGroupDelimiter);
      foreach (ISettingsPropertyDefinition settingProperty in this.SettingProperties)
        propertyGroupDefinition.Add((ISettingsPropertyDefinition) settingProperty.Clone(keepRefs));
      foreach (SettingsPropertyGroupDefinition subGroup in this.SubGroups)
        propertyGroupDefinition.Add(subGroup.Clone(keepRefs));
      return propertyGroupDefinition;
    }

    private class DefaultPropertyGroupDefinition : IPropertyGroupDefinition
    {
      public string GroupName => SettingsPropertyGroupDefinition.DefaultGroupName;

      public int GroupOrder => 0;
    }
  }
}
